package com.sharing.service;

import com.sharing.common.Page;
import com.sharing.common.Result;
import com.sharing.domain.ProductionLike;
import com.sharing.domain.User;
import com.sharing.domain.UserLike;
import com.sharing.domain.UserProduction;
import com.sharing.mapper.ProductionLikeMapper;
import com.sharing.mapper.UserLikeMapper;
import com.sharing.mapper.UserMapper;
import com.sharing.mapper.UserProductionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.jws.soap.SOAPBinding;
import java.util.List;

/**
 * @author MingTian
 * @version 1.0
 * @description TODO
 * @date 2023/4/3 21:15
 */
@Service
public class ProductionLikeService {
    @Autowired
    private ProductionLikeMapper productionLikeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserProductionMapper userProductionMapper;

    /**
     * 根据作品id查询点赞的用户信息的业务层方法
     * @param productionId
     * @param page
     * @param size
     * @return
     */
    public Result<Page<User>> selectLikeUsersByProductionId(Integer productionId, Integer page, Integer size) {
        // 校验该作品id是否存在
        UserProduction userProduction = userProductionMapper.selectProductionById(productionId);

        if (userProduction == null) {
            return new Result<>(1, "该作品不存在");
        }

        // 查询该作品的点赞用户
        List<Integer> userIds = productionLikeMapper.selectLikeUsersIdsByProductionId(productionId, (page - 1) * size, size);
        List<User> users = userMapper.selectUsersByIds(userIds, 1, Integer.MAX_VALUE);

        // 查询点赞用户总数
        Integer total = productionLikeMapper.selectLikeUsersNumByProductionId(productionId);

        return new Result<>(0, new Page<>(total, size, users));
    }

    /**
     * 新增作品点赞业务层方法
     * @param productionLike
     * @return
     */
    public Result<ProductionLike> insertProductionLike(ProductionLike productionLike) {
        // 参数校验
        if (productionLike.getProductionId() == null && productionLike.getUserId() == null) {
            return new Result<>(1, "参数不完整");
        }

        // 校验该作品点赞信息是否已经存在
        Integer rows = productionLikeMapper.selectLikeUsersNumByProductionLike(productionLike);

        if (rows >= 1) {
            return new Result<>(1, "该点赞信息已经存在，请勿重复添加");
        }

        Integer rows1 = productionLikeMapper.insertProductionLike(productionLike);
        if (rows1 != 1) {
            return new Result<>(1, "插入失败");
        }

        return new Result<>(0, productionLike);
    }

    /**
     * 删除作品点赞
     * @param id
     * @return
     */
    public Result<Integer> deleteProductionLikeById(Integer id) {
        ProductionLike productionLike = productionLikeMapper.selectLikeUserById(id);
        if (productionLike == null) {
            return new Result<>(1, "该点赞不存在");
        }

        Integer rows = productionLikeMapper.deleteProductionLikeById(id);

        if (rows == 0) {
            return new Result<>(1, "删除失败");
        }

        return new Result<>(0, rows);
    }
}
